/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import type {HostComponent, ViewProps} from 'react-native';
import type {
  DirectEventHandler,
  Int32,
  WithDefault,
} from 'react-native/Libraries/Types/CodegenTypes';
import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands';
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';

export interface MovingPhotoNativeProps extends ViewProps {
  // 媒体库资源路径
  source: string;
  // 设置是否静音
  isMuted?: boolean;
  // 设置动态照片显示模式
  resizeMode?: WithDefault<'contain' | 'cover' | 'stretch' | 'center', 'cover'>;
  // 区间播放开始时间，单位：ms。取值范围：大于等于0
  startTime?: Int32;
  // 区间播放结束时间，单位：ms。取值范围：大于startTime。
  endTime?: Int32;
  // 设置自动播放，自动播放一遍视频
  isAutoPlay?: boolean;
  // 设置循环播放，重复播放视频。repeatPlay与autoPlay及长按播放互斥，repeatPlay设置时，autoPlay和长按播放均不生效
  isRepeatPlay?: boolean;
  // 设置该图片是否支持AI分析，当前支持主体识别、文字识别和对象查找等功能。
  enableAnalyzer?: boolean;
  // 动态照片加载完成图片时触发该事件。
  onComplete?: DirectEventHandler<Readonly<{}>>;
  // 播放时触发该事件。
  onStart?: DirectEventHandler<Readonly<{}>>;
  // 播放暂停时触发该事件。
  onPause?: DirectEventHandler<Readonly<{}>>;
  // 播放结束时触发该事件。
  onFinish?: DirectEventHandler<Readonly<{}>>;
  // 播放失败时触发该事件。
  onError?: DirectEventHandler<Readonly<{}>>;
  // 播放停止时触发该事件(当stop()方法被调用后触发)。
  onStop?: DirectEventHandler<Readonly<{}>>;
}

export type MovingPhotoComponentType = HostComponent<MovingPhotoNativeProps>;

export interface NativeCommands {
  startPlayback: (viewRef: React.ElementRef<MovingPhotoComponentType>) => void;
  stopPlayback: (viewRef: React.ElementRef<MovingPhotoComponentType>) => void;
  refreshMovingPhoto: (
    viewRef: React.ElementRef<MovingPhotoComponentType>,
  ) => void;
}

export const Commands: NativeCommands = codegenNativeCommands<NativeCommands>({
  supportedCommands: ['startPlayback', 'stopPlayback', 'refreshMovingPhoto'],
});

export default codegenNativeComponent<MovingPhotoNativeProps>(
  'RNMovingPhotoView',
) as MovingPhotoComponentType;
